package com.sanqi.web.controller.work;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sanqi.common.annotation.Log;
import com.sanqi.common.core.controller.BaseController;
import com.sanqi.common.core.domain.AjaxResult;
import com.sanqi.common.enums.BusinessType;
import com.sanqi.work.domain.WorkStepLevel;
import com.sanqi.work.domain.WorkStepLevelPrice;
import com.sanqi.work.service.IWorkStepLevelPriceService;
import com.sanqi.work.service.IWorkStepLevelService;

import cn.hutool.core.collection.CollUtil;

import com.sanqi.common.utils.SQMapUtil;
import com.sanqi.common.utils.poi.ExcelUtil;
import com.sanqi.common.core.page.TableDataInfo;

/**
 * 工作计件阶梯Controller
 * 
 * @author duany
 * @date 2025-10-08
 */
@RestController
@RequestMapping("/work/level")
@Api(tags="工作计件阶梯")
public class WorkStepLevelController extends BaseController
{
    @Autowired
    private IWorkStepLevelService workStepLevelService;
    @Autowired
    private IWorkStepLevelPriceService workStepLevelPriceService;

    /**
     * 查询工作计件阶梯列表
     */
    @PreAuthorize("@ss.hasPermi('work:level:list')")
    @GetMapping("/list")
    @ApiOperation(value="工作计件阶梯-查询工作计件阶梯列表", notes="工作计件阶梯-查询工作计件阶梯列表")
    public TableDataInfo list(WorkStepLevel workStepLevel)
    {
        startPage();
        List<WorkStepLevel> list = workStepLevelService.selectWorkStepLevelList(workStepLevel);
        if(CollUtil.isNotEmpty(list)) {
            List<String> idList = list.stream().map(WorkStepLevel::getId).collect(Collectors.toList());
            List<WorkStepLevelPrice> priceList = workStepLevelPriceService.queryByLevelIds(idList);
            Map<String, List<WorkStepLevelPrice>> priceMap = SQMapUtil.ListToMapList(priceList, WorkStepLevelPrice::getLevelId);
            for (WorkStepLevel level : list) {
                String id = level.getId();
                List<WorkStepLevelPrice> priList = priceMap.get(id);
                level.setPriceList(priList == null ? Collections.emptyList() : priList); 
            }
        }
        return getDataTable(list);
    }
    
    /**
     * 查询工作计件阶梯列表
     */
    @PreAuthorize("@ss.hasPermi('work:level:list')")
    @GetMapping("/priceList/{id}")
    @ApiOperation(value="工作计件阶梯-查询工作计件阶梯价格", notes="工作计件阶梯-查询工作计件阶梯价格")
    public AjaxResult priceList(@PathVariable("id") String id)
    {
        List<WorkStepLevelPrice> list = workStepLevelPriceService.queryByLevelId(id);
        return success(list);
    }

    /**
     * 导出工作计件阶梯列表
     */
    @PreAuthorize("@ss.hasPermi('work:level:export')")
    @Log(title = "工作计件阶梯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value="工作计件阶梯-导出工作计件阶梯列表", notes="工作计件阶梯-导出工作计件阶梯列表")
    public void export(HttpServletResponse response, WorkStepLevel workStepLevel)
    {
        List<WorkStepLevel> list = workStepLevelService.selectWorkStepLevelList(workStepLevel);
        ExcelUtil<WorkStepLevel> util = new ExcelUtil<WorkStepLevel>(WorkStepLevel.class);
        util.exportExcel(response, list, "工作计件阶梯数据");
    }

    /**
     * 获取工作计件阶梯详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:level:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value="工作计件阶梯-获取工作计件阶梯详细信息", notes="工作计件阶梯-获取工作计件阶梯详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(workStepLevelService.selectWorkStepLevelById(id));
    }

    /**
     * 新增工作计件阶梯
     */
    @PreAuthorize("@ss.hasPermi('work:level:add')")
    @Log(title = "工作计件阶梯", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="工作计件阶梯-新增工作计件阶梯", notes="工作计件阶梯-新增工作计件阶梯")
    public AjaxResult add(@RequestBody WorkStepLevel workStepLevel)
    {
        return toAjax(workStepLevelService.insertWorkStepLevel(workStepLevel));
    }

    /**
     * 修改工作计件阶梯
     */
    @PreAuthorize("@ss.hasPermi('work:level:edit')")
    @Log(title = "工作计件阶梯", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="工作计件阶梯-修改工作计件阶梯", notes="工作计件阶梯-修改工作计件阶梯")
    public AjaxResult edit(@RequestBody WorkStepLevel workStepLevel)
    {
        return toAjax(workStepLevelService.updateWorkStepLevel(workStepLevel));
    }

    /**
     * 删除工作计件阶梯
     */
    @PreAuthorize("@ss.hasPermi('work:level:remove')")
    @Log(title = "工作计件阶梯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	@ApiOperation(value="工作计件阶梯-删除工作计件阶梯", notes="工作计件阶梯-删除工作计件阶梯")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        int count = workStepLevelService.deleteWorkStepLevelByIds(ids);
        if(0 != count) {
            for (String levelId : ids) {
                this.workStepLevelPriceService.removeByLevelId(levelId);
            }
        }
        return toAjax(count);
    }
    
    /**
     * 添加工作计件阶梯价格
     */
    @PreAuthorize("@ss.hasPermi('work:level:addPrice')")
    @PostMapping("/addPrice")
    @ApiOperation(value="工作计件阶梯-添加工作计件阶梯价格", notes="工作计件阶梯-添加工作计件阶梯价格")
    public AjaxResult priceList(@RequestBody WorkStepLevelPrice price)
    {
        return toAjax(this.workStepLevelPriceService.save(price));
    }
    
    /**
     * 修改工作计件阶梯价格
     */
    @PreAuthorize("@ss.hasPermi('work:level:editPrice')")
    @PutMapping("/editPrice")
    @ApiOperation(value="工作计件阶梯-添加工作计件阶梯价格", notes="工作计件阶梯-添加工作计件阶梯价格")
    public AjaxResult editPrice(@RequestBody WorkStepLevelPrice price)
    {
        return toAjax(this.workStepLevelPriceService.updateById(price));
    }
    
    /**
     * 修改工作计件阶梯价格
     */
    @PreAuthorize("@ss.hasPermi('work:level:removePrice')")
    @DeleteMapping("/removePrice/{ids}")
    @ApiOperation(value="工作计件阶梯-添加工作计件阶梯价格", notes="工作计件阶梯-添加工作计件阶梯价格")
    public AjaxResult removePrice(@PathVariable String[] ids)
    {
        int count = workStepLevelPriceService.deleteWorkStepLevelPriceByIds(ids);
        return toAjax(count);
    }
}
